package com.changgou.worder.controller;

import com.changgou.entity.Result;
import com.changgou.order.feign.OrderFeign;
import com.changgou.order.pojo.Order;
import com.changgou.pay.feign.WxPayFeign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Map;

@Controller
@RequestMapping("/wwxpay")
public class PayController {

    @Autowired
    private OrderFeign orderFeign;
    @Autowired
    private WxPayFeign wxPayFeign;

    @GetMapping("/topaysuccess")
    public String topaysuccess(String payMoney,Model model){
        model.addAttribute("payMoney",payMoney);
        return "paysuccess";
    }

    /**
     * 微信支付二维码
     * @param orderId
     * @return
     */
    @GetMapping
    public String wxPay(String orderId, Model model){
        //根据orderId查询订单
        Order order = orderFeign.findById(orderId).getData();
        if(order==null){
            return "fail";
        }


        //判断订单是否支付
        if(!"0".equals(order.getPayStatus())){
            return "fail";
        }

        Map payMap = (Map) wxPayFeign.nativePay(orderId,order.getPayMoney()).getData();
        if(payMap==null){
            return "fail";
        }


        payMap.put("payMoney",order.getPayMoney());
        payMap.put("orderId",orderId);

        model.addAllAttributes(payMap);
        return "wxpay";

    }


}
